package com.wcs.util;

import com.wcs.domain.Book;
import com.wcs.parse.HtmlParse;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Eternal_L on 2017/11/24.
 */
public class URLFecter {

    public static List<Book> URLParser(String url) throws IOException {
        List<Book> data = new ArrayList<>();
        CloseableHttpResponse res = HttpUtils.getHtml(url);
        try {
            // 获取网站响应的html页面

            // 获取响应状态码，只有状态码正常时才获取数据
            int statusCode = res.getStatusLine().getStatusCode();
            System.out.println(res.getStatusLine());
            if (statusCode == HttpStatus.SC_OK){
                HttpEntity entity = res.getEntity();
                System.out.println(entity.getContentLength());

                if (entity.getContentLength() != -1){
                    String htmlEntity = EntityUtils.toString(entity, "UTF-8");
                    data = HtmlParse.getData(htmlEntity);
                    EntityUtils.consume(res.getEntity());
                }
            } else {
                // 直接消耗掉实体数据
                EntityUtils.consume(res.getEntity());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            res.close();
        }
        return data;
//        https://www.cnblogs.com/cy163/archive/2013/02/14/2912627.html
    }

}
